package com.data.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.config.dao.EquStateConfigDao;
import com.config.dao.EquipmentDao;
import com.config.dao.LineConfigDao;
import com.config.dao.ShiftConfigDao;
import com.config.entity.EquStateConfig;
import com.config.entity.Equipment;
import com.config.entity.LineConfig;
import com.config.entity.ShiftConfig;
import com.data.dao.BaseDataDao;
import com.data.dao.StateMinDao;
import com.data.dao.StateShiftDao;
import com.data.entity.StateMin;
import com.data.entity.StateShift;

@Service("stateShiftService")
public class StateShiftServiceImpl implements StateShiftService{
	@Resource(name="baseDataDao")
	private BaseDataDao baseDataDao;
	@Resource(name="lineConfigDao")
	private LineConfigDao lineConfigDao;
	@Resource(name="equipmentDao")
	private EquipmentDao equipmentDao;
	@Resource(name="stateShiftDao")
	private StateShiftDao stateShiftDao;
	@Resource(name="stateMinDao")
	private StateMinDao stateMinDao;
	@Resource(name="shiftConfigDao")
	private ShiftConfigDao shiftConfigDao;
	@Resource(name="equStateConfigDao")
	private EquStateConfigDao equStateConfigDao;
	/**
	 * 根据日期提取设备状态班次记录
	 * @param belongDate
	 */
	@Override
	public void saveStateShift(Date belongDate) {
		try{
				SimpleDateFormat sdft = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				SimpleDateFormat sdt = new SimpleDateFormat("HH:mm:ss");
				List<EquStateConfig> states=equStateConfigDao.findAll();
				List<LineConfig> lines=lineConfigDao.findAllLine();
				if(lines.size()!=0){
					for(LineConfig line:lines){
						// 查询班次
						ShiftConfig config=shiftConfigDao.findShiftConfigByDate(line.getId(), belongDate);
						List<Equipment> equs=equipmentDao.listByLine(line.getId());
						if(equs.size()!=0){
							for(Equipment e:equs){
								if(states.size()!=0){
									for(EquStateConfig state:states){
										double duration=0.0; double relativeComp=0.0;
										if(config!=null){
											 StateShift shift=stateShiftDao.getStateShiftByIdAndDate(e.getId(), config.getId(),state.getStateType(), sdf.format(belongDate));
											 boolean isNew=false;
											 if(shift==null){
												shift=new StateShift();
												shift.setEquId(e.getId());
												shift.setShiftId(config.getId());
												shift.setShiftName(config.getShiftName());
												shift.setStateType(state.getStateType());
												shift.setBelongDate(sdf.parse(sdf.format(belongDate)));
												isNew=true;
											}	
											Calendar ca=Calendar.getInstance();
											ca.setTime(config.getStartTime());
											String date=sdf.format(belongDate)+" "+sdt.format(ca.getTime());
											List<StateMin> mins=stateMinDao.getStateMins(e.getId(), state.getStateType(), date, sdft.format(belongDate));
											if(mins.size()!=0){
												 for(StateMin min:mins){
													 duration+=min.getDuration();
													 relativeComp+=min.getRelativeComp();
												 }
											}
											shift.setDuration(duration);
											shift.setRelativeComp(relativeComp);
											if(isNew){
												stateShiftDao.save(shift);
											}else{
												stateShiftDao.update(shift);
											}
										}
									}
								}
							}
						}
					}
				}
		      } catch (ParseException e1) {
		         e1.printStackTrace();
       }
   }
}
